package com.songtech.auth.user.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.songtech.auth.role.model.Role;
import com.songtech.auth.user.model.User;
import com.songtech.auth.user.service.UserService;
import com.songtech.base.common.BaseConstant;
import com.songtech.base.common.JsonResult;
import com.songtech.base.exception.BusinessException;

@Controller
@RequestMapping("auth/user")
public class AuthUserController {

	@Autowired
	private UserService userService;
	
	@RequestMapping("/add")
	@ResponseBody
	public Object addUser(User user){
		int result = userService.addUser(user);
		if(result==-2){
			throw new BusinessException("已经存在相同登录名的用户");
		}else if(result==-1){
			throw new BusinessException("用户密码为空");
		}else if (result==0) {
			throw new BusinessException("用户登录名为空");
		}
		return 1;
	}
	
	@RequestMapping("/edit")
	@ResponseBody
	public Object editUser(User user){
		int result = userService.updateUser(user);
		return result;
	}
	
	/**
	 * 通过用户ID，查询用户详情
	 * @param user
	 * @return
	 */
	@RequestMapping("/getUser")
	@ResponseBody
	public User getUser(User user) {
		User result = null;
		if(!StringUtils.isEmpty(user.getId())){
			result = userService.queryUserById(user.getId());
		}
		return result;
	}
	
	/**
	 * 通过用户登录名，查询用户拥有的角色
	 * @param user
	 * @return
	 */
	@RequestMapping("/getUserRoles")
	@ResponseBody
	public Object getUserRoles(User user) {
		List<Role> result = null;
		if(!StringUtils.isEmpty(user.getLoginName())){
			result = userService.getUserRoles(user.getLoginName());
		}
		return result;
	}
	/**
	 * 查询所有用户
	 * @return
	 */
	@RequestMapping("/getAll")
	@ResponseBody
	public Object getAll(User user,String page, String limit) {
		
		int pageNum = 0;
		int pageSize = 10;
		if(!StringUtils.isEmpty(page)){
			pageNum = Integer.valueOf(page);
		}
		if(!StringUtils.isEmpty(limit)){
			pageSize = Integer.valueOf(limit);
		}
		PageInfo<User> users = userService.getAll(user,pageNum,pageSize);
		JsonResult result = new JsonResult(users);
		result.status = BaseConstant.SUCCESS_CODE;
		result.msg = BaseConstant.SUCCESS;
		return result;
	}
	/**
	 * 查询所有有效用户
	 * @return
	 */
	@RequestMapping("/getAllUsers")
	@ResponseBody
	public Object getAllUsers() {
		return userService.getAllUser();
	}
	
	/**
	 * 启用用户
	 * @return
	 */
	@RequestMapping("/open")
	@ResponseBody
	public Object enabledUser(String userId) {
		return userService.enabledUser(userId);
	}
	/**
	 * 停用用户
	 * @return
	 */
	@RequestMapping("/stop")
	@ResponseBody
	public Object disabledUser(String userId) {
		return userService.disabledUser(userId);
	}
	
	/**
	 * 删除用户
	 * @param userId
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Object deleteUser(String userId) {
		return userService.delete(userId);
	}
}
